package auto.setting;


import auto.common.ImageConvertedBase64;
import auto.common.Login;
import auto.common.RandomStringGenerate;
import auto.common.Registartion;
import auto.device.Phione;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.util.*;
import org.openqa.selenium.NoSuchElementException;

//这个类里面是检测个人信息的
public class PersonalDetails {

//feature1：检测当前激活账号的个人信息是不是加载了
    public String changeTextPhoto() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //检测激活账号数据是否加载，数据没有加载就直接报错
        if (activateAccountDataLoadingInspect(wait, driver)) {
            System.out.println("当前激活账号数据没有加载");
            return "当前激活账号数据没有加载";
        }


        System.out.println("当前账号显示头像");
        driver.quit();
        return "当前账号显示头像";
    }


//feature2:编辑头像和移除头像按钮是否生效
    public String avatarEditButton() throws InterruptedException, IOException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        String commonPartReturn=publicPartFunction1(driver,wait);
        if(!commonPartReturn.equals("正常")){
            return commonPartReturn;
        }


        String photoPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeImage";

        //将图片进行保存
        WebElement photoOldElement = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(photoPath)));
        File photoOld = photoOldElement.getScreenshotAs(OutputType.FILE);

        // 保存截图到指定路径
        File destinationFile1 = new File("src/main/resources/image/photoOld.png");
        FileUtils.copyFile(photoOld, destinationFile1);

        //获取旧图片的Base64
        String photoOld64 = ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/photoOld.png");

        //检测移除照片的按钮是不是消失了
        try {
            String removePhotoButtonPath = "**/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeImage";
            WebElement removePhotoButton = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(removePhotoButtonPath)));
            removePhotoButton.click();

            //这个地方需要判断现在的这个
            if (driver.findElements(AppiumBy.iOSClassChain(removePhotoButtonPath)).size() > 0) {
                System.out.println("点击移除按钮后，移除按钮未消失");
                driver.quit();
                return "点击移除按钮后，移除按钮未消失";
            }
        } catch (TimeoutException e) {
            System.out.println("移除照片按钮消失");
            driver.quit();
            return "移除照片按钮消失";
        }

        //将图片进行保存
        WebElement photoNewElement = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(photoPath)));
        File photoNew = photoNewElement.getScreenshotAs(OutputType.FILE);

        // 保存截图到指定路径
        File destinationFile2 = new File("src/main/resources/image/photoNew.png");
        FileUtils.copyFile(photoNew, destinationFile2);

        //获取新图片的Base64
        String photoNew64 = ImageConvertedBase64.encodeImageToBase64("src/main/resources/image/photoNew.png");

        if (photoNew64.equals(photoOld64)) {
            System.out.println("图片相同，移除失败");
            driver.quit();
            return "图片相同，移除失败";
        }

        //点击设置
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("set_button"))).click();

        //点击完成
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();

        System.out.println("编辑头像按钮和移除头像按钮显示正常");
        driver.quit();
        return "编辑头像按钮和移除头像按钮显示正常";
    }


//feature3：编辑头像-按钮跳转检测
    public String buttonJump() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        String commonPartReturn=publicPartFunction1(driver,wait);
        if(!commonPartReturn.equals("正常")){
            return commonPartReturn;
        }

        //检测文本按钮的跳转
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"文本\"`]"))).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("取消"))).click();
        }catch (TimeoutException e){
            System.out.println("文本按钮跳转或跳转后取消失败");
            driver.quit();
            return "文本按钮跳转或跳转后取消失败";
        }

        //检测图片按钮的跳转
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"图片\"`]"))).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("zl navClose"))).click();
        }catch (TimeoutException e){
            System.out.println("图片按钮跳转或跳转后取消失败");
            driver.quit();
            return "图片按钮跳转或跳转后取消失败";
        }

        //检测相机按钮的跳转
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"相机\"`]"))).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"取消\"`]"))).click();
        }catch (TimeoutException e){
            System.out.println("相机按钮跳转或跳转后取消失败");
            driver.quit();
            return "相机按钮跳转或跳转后取消失败";
        }

        //点击取消返回上一个页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("cancelButton"))).click();

        //点击返回到设置主页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        System.out.println("文本、图片、相机 按钮跳转成功");
        driver.quit();
        return "编辑头像-按钮跳转正常";
    }


//feature4：编辑头像-使用不同的方式设置头像
    public String setProfilePicture() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;
        System.out.println("请观察更换头像时头像是否更换成功");

        //这个变量是方便选择不同的方式更换头像，以及公共代码的复用
        int options=1;
        while(options<5){
            String commonPartReturn=publicPartFunction1(driver,wait);
            if(!commonPartReturn.equals("正常")){
                return commonPartReturn;
            }
            switch(options){
                case 1:
                    usePresetAvatar(driver,wait);
                    System.out.println("使用预设头像设置成功");
                    Thread.sleep(500);
                    break;
                case 2:
                    useTextAvatar(driver,wait);
                    System.out.println("使用文本头像设置成功");
                    Thread.sleep(500);
                    break;
                case 3:
                    usePhotoAlbum(driver,wait);
                    System.out.println("使用相册设置头像成功");
                    Thread.sleep(500);
                    break;
                case 4:
                    useCamera(driver,wait);
                    System.out.println("使用相机设置头像成功");
                    Thread.sleep(500);
                    break;
                default:
                    break;
            }
            //点击设置
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("set_button"))).click();

            //点击完成
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();
            options++;
        }


        System.out.println("使用不同类型的方式设置头像完成");
        driver.quit();
        return "使用不同类型的方式设置头像完成";
    }


//feature5:编辑头像-移除头像后取消本次操作
    public String cancelAvatarRemove() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        String commonPartReturn=publicPartFunction1(driver,wait);
        if(!commonPartReturn.equals("正常")){
            return commonPartReturn;
        }

        //点击移除按钮
        String removePhotoButtonPath = "**/XCUIElementTypeWindow/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeImage";
        WebElement removePhotoButton = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(removePhotoButtonPath)));
        removePhotoButton.click();

        //点击取消
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("cancelButton"))).click();

        //确认丢弃
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("放弃"))).click();

        //点击返回
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        System.out.println("取消移除头像成功");
        driver.quit();
        return "取消移除头像成功";
    }


//feature6:个人资料-昵称的检测
    public String nickNameDetection() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有加载");
            return "当前激活账户数据没有记载";
        }

        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        //保存激活用户的昵称和用户名，下标1是昵称
        WebElement user1 = driver.findElement(AppiumBy.iOSClassChain(firstUser));
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
        String nickName=nickNameAndUSerName.get(1).getAttribute("value");
        user1.click();

        //昵称的文本框
        WebElement nickTextBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField")));

        //比较文本框中的昵称和设置页面的是否相同
        if(!nickTextBox.getAttribute("value").equals(nickName)){
            System.out.println("设置界面和详情界面的昵称不一致");
            return "设置界面和详情界面的昵称不一致";
        }

        //向文本框中输入字符
        nickTextBox.clear();

        //清空之后如果也出现了完成并且能够点击，那就是错误的
        try{
            driver.findElement(AppiumBy.accessibilityId("save_button"));
            System.out.println("文本框清空之后，不能出现完成");
            return "文本框清空之后，不能出现完成";
        }catch (NoSuchElementException e){
            //找不到是正常的情况，不用进行处理
        }

        String randomString= RandomStringGenerate.generateRandomString(31);
        nickTextBox.sendKeys(randomString);

        //看一下输入的内容超出是否会有提示
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("昵称最多30个字符")));
            System.out.println("超出提示限制显示正常");
        }catch (TimeoutException e){
            System.out.println("输入超出限制提示消失");
            return "输入超出限制提示消失";
        }

        //点击右上角的完成
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();
            System.out.println("完成按钮显示正常");
        }catch (TimeoutException e){
            System.out.println("右上角完成按键消失");
            return "右上角完成按钮消失";
        }

        //返回之后查看是不是数据加载异常
        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有记载");
            return "当前激活账户数据没有记载";
        }

        //点击完成之后，返回到设置页面，现在查看输入的内容是31个字符还是输入了30个字符
        List<WebElement> nickNameAndUSerName1=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //判断设置页面显示的昵称是多少个字符
        if(!nickNameAndUSerName1.get(1).getAttribute("value").equals(randomString.substring(0,randomString.length()-1))){
            System.out.println("昵称修改失败");
            return "昵称修改失败";
        }
        System.out.println("设置的昵称实际为30字符");

        System.out.println("昵称修改正常");
        driver.quit();
        return "昵称修改正常";
    }


//feature7:个人资料-个人简介的检测              ---bug未修复
    public String personalProfileDetection() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //检测数据加载并进入详情页面
        String commonPartReturn=publicPartFunction2(driver,wait);
        if(!commonPartReturn.equals("正常")){
            return commonPartReturn;
        }

        //用来保存个人简介的字符串
        String personalProfileOld;
        //首先判断是不是空消息
        try{
            driver.findElement(AppiumBy.accessibilityId("添加一些关于你的个人介绍"));
            System.out.println("当前个人介绍不存在");
        }catch(NoSuchElementException e){
            System.out.println("当前个人介绍是存在的");
            //保存个人简介
            personalProfileOld=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextView"))).getAttribute("value");


            //点击一键清空个人简介
            try{
                driver.findElement(AppiumBy.className("XCUIElementTypeTextView")).click();
                wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon alldelete day"))).click();
            }catch(TimeoutException e1){
                System.out.println("一键清空按钮消失");
                return "一键清空按钮消失";
            }

            //查看是否成功清空
            try{
                driver.findElement(AppiumBy.accessibilityId("添加一些关于你的个人介绍"));
            }catch(NoSuchElementException e2){
                System.out.println("个人简介一键清空失败");
                return "个人简介一键清空失败";
            }
        }


        //定位个人简介文本框
        WebElement personalIntroduction=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextView")));

        //新的个人介绍
        String personalProfileNew=RandomStringGenerate.generateRandomString(72);
        personalIntroduction.sendKeys(personalProfileNew);
        System.out.println("输入了72个字符");

        //查看超出内容后有没有提示
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("个人简介最多70个字符")));
            if(driver.findElements(AppiumBy.accessibilityId("save_button")).size()>0){
                System.out.println("输入超出限制的时候不允许出现完成按钮");
                return "输入超出限制的时候不允许出现完成按钮";
            }
        }catch(TimeoutException e){
            System.out.println("输入超出长度限制，没有出现提示");
            return "输入超出长度限制，没有出现提示";
        }

        //删除一个字符才允许出现完成按钮
        driver.findElement(AppiumBy.accessibilityId("delete")).click();

        //点击完成按钮完成设置
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();
        }catch(TimeoutException e){
            System.out.println("完成按钮消失");
            return "完成按钮消失";
        }

        //然后再次点击进来，查看是否设置成功
        //检测数据加载并进入详情页面
        String commonPartReturn1=publicPartFunction2(driver,wait);
        if(!commonPartReturn1.equals("正常")){
            return commonPartReturn1;
        }

        //查看当前保存的是什么
        personalProfileOld=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextView"))).getAttribute("value");

        //和随机的那个字符串进行对比，如果是70个字符就修改成功
        if(!personalProfileOld.equals(personalProfileNew.substring(0,personalProfileNew.length()-2))){
            System.out.println("个人简介长度超过70，修改失败");
            return "个人简介长度超过70，修改失败";
        }

        //点击返回
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        //返回之后再次点击进入
        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUser))).click();

        //然后现在判断是不是能设置空的
        //点击一键清空个人简介
        try{
            driver.findElement(AppiumBy.className("XCUIElementTypeTextView")).click();
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("icon alldelete day"))).click();
        }catch(TimeoutException e1){
            System.out.println("一键清空按钮消失");
            return "一键清空按钮消失";
        }

        //点击完成按钮完成设置
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();
        }catch(TimeoutException e){
            System.out.println("完成按钮消失");
            return "完成按钮消失";
        }

        //再次点击进入查看是不是空的，如果不是空的就是错的
        //查看是否成功清空
        try{
            driver.findElement(AppiumBy.accessibilityId("添加一些关于你的个人介绍"));
        }catch(NoSuchElementException e2){
            System.out.println("个人简介一键清空失败");
            return "个人简介一键清空失败";
        }

        //点击返回
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        System.out.println("个人简介修改成功");
        driver.quit();
        return "个人简介修改成功";
    }


//feature8:个人资料-用户名检测
    public String userNameDetection() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //检测页面数据的加载
        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有记载");
            return "当前激活账户数据没有记载";
        }

        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        //保存激活用户的昵称和用户名，下标0是用户名和手机号码
        WebElement user1 = driver.findElement(AppiumBy.iOSClassChain(firstUser));
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //提取设置页面的用户名
        String userName=nickNameAndUSerName.get(0).getAttribute("value");
        System.out.println("设置页面的用户名："+userName);
        String userNameKid=userName.substring(userName.indexOf('@'));
        user1.click();

        //避免简介过长，上划一次
        Map<String, Object> argsUp = new HashMap<>();
        argsUp.put("direction", "up");
        driver.executeScript("mobile: swipe", argsUp);

        //点击进入后检查设置页面和详情页面的用户名是否一致,以及详情页面的用户名是否出现
        try{
            WebElement userName1=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name CONTAINS '@'`]")));
            System.out.println("详情页面的用户名:"+userNameKid);
            if(!userName1.getAttribute("value").equals(userNameKid)){
                System.out.println("设置页面和详情页面用户名不一致");
                driver.quit();
                return "设置页面和详情页面用户名不一致";
            }
            userName1.click();
        }catch(TimeoutException e){
            System.out.println("详情页面的用户名消失");
            driver.quit();
            return "详情页面的用户名消失";
        }

        //检测
        //点击一键清空用户名
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("清除文本"))).click();
        }catch(TimeoutException e1){
            System.out.println("一键清空按钮消失");
            driver.quit();
            return "一键清空按钮消失";
        }

        //查看是否成功清空
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"请输入用户名\"`]")));
            System.out.println("用户名清空成功1");
        }catch(TimeoutException e2){
            System.out.println("用户名一键清空失败");
            driver.quit();
            return "用户名一键清空失败";
        }

        //查看提示是否出现
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"用户名最少5个字符\"`]")));
            System.out.println("没有用户名时提示出现");
        }catch(TimeoutException e){
            System.out.println("\"最少五个字符的提示\"提示消失");
            driver.quit();
            return "\"最少五个字符的提示\"提示消失";
        }

        //在文本框中进行输入
        WebElement userNameTextBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeCell")));
        String shortNewUserName= RandomStringGenerate.generateRandomString(4);
        userNameTextBox.sendKeys(shortNewUserName);

        //查看提示是否出现
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"用户名最少5个字符\"`]")));
            System.out.println("输入4个字符后。提示正常出现");
        }catch(TimeoutException e2){
            System.out.println("\"最少五个字符的提示\"提示消失");
            driver.quit();
            return "\"最少五个字符的提示\"提示消失";
        }

        //现在输入5个字符
        //点击一键清空用户名
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("清除文本"))).click();
        }catch(TimeoutException e){
            System.out.println("一键清空按钮消失");
            driver.quit();
            return "一键清空按钮消失";
        }

        //查看是否成功清空
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeTextField[`value == \"请输入用户名\"`]")));
            System.out.println("用户名清空成功2");
        }catch(TimeoutException e){
            System.out.println("用户名一键清空失败");
            driver.quit();
            return "用户名一键清空失败";
        }
        //输入新的5个字符
        shortNewUserName=RandomStringGenerate.generateRandomString(5);
        userNameTextBox.sendKeys(shortNewUserName);

        //输入五个字符之后，提示消失，并且完成按钮需要出现
        try{
            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"用户名最少5个字符\"`]"));
            System.out.println("\"最少五个字符的提示\"提示没有消失");
            driver.quit();
            return "\"最少五个字符的提示\"提示没有消失";
        }catch(NoSuchElementException e){
            //提示消失属于正常不做处理
            System.out.println("输入五个字符后提示消失");
        }

        //判断完成按钮是否出现了，出现了就是正常的
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button")));
            System.out.println("输入五个字符后完成按钮正常出现");
        }catch(TimeoutException e){
            System.out.println("输入五个字符之后完成没有出现");
            driver.quit();
            return "输入五个字符之后完成没有出现";
        }


        //超出限制的时候不能出现完成，重新清空
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("清除文本"))).click();
        String longNewUserName= RandomStringGenerate.generateRandomString(21);
        userNameTextBox.sendKeys(longNewUserName);


        //查看输入超出限制的提示是否出现
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"用户名太长\"`]")));
            //出现之后删除一位
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("delete"))).click();

            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button")));
        }catch(TimeoutException e){
            System.out.println("用户名输入超出限制提示未出现或者输入20位字符完成按钮未出现");
            driver.quit();
            return "用户名输入超出限制提示未出现或者完成按钮未出现";
        }

        //删除一位之后就需要出现完成按钮，如果没有出现就是错误的
        try{
            wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("save_button"))).click();
        }catch(TimeoutException e){
            System.out.println("完成按钮没有出现");
            driver.quit();
            return "完成按钮没有出现";
        }

        //返回之后查看详情页面显示的是否和longName相同,这个是设置后详情页面显示的
        WebElement userName1=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name CONTAINS '@'`]")));


        String anObject = ("@" + longNewUserName.substring(0, longNewUserName.length() - 1)).toLowerCase();
        System.out.println("新设置的用户名："+anObject);
        System.out.println("详情页面显示的新用户名："+userName1.getAttribute("value"));

        if(!userName1.getAttribute("value").equals(anObject)){
            System.out.println("用户名设置失败");
            driver.quit();
            return "用户名设置失败";
        }

        //再次点击返回，到设置页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        //返回到设置页面进行检查
        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有记载");
            return "当前激活账户数据没有记载";
        }

        Thread.sleep(1000);
        firstUser="**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        List<WebElement> nickNameAndUSerNameNew=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //提取设置页面的用户名
        String userNameNew=nickNameAndUSerNameNew.get(0).getAttribute("value");
        String userNameKidNew=userNameNew.substring(userNameNew.indexOf('@'));
        System.out.println("设置页面的用户名："+userNameKidNew);


        if(!userNameKidNew.equals(anObject)){
            System.out.println("设置页面显示的和输入的用户名不一致");
            driver.quit();
            return "设置页面显示的和输入的用户名不一致";
        }

        System.out.println("修改用户名正常");
        driver.quit();
        return "修改用户名正常";
    }


//feature9：个人资料-账户设置检测默认是中国大陆的手机号
    public String changePhone(String newPhone) throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //检测页面数据的加载
        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有记载");
            return "当前激活账户数据没有记载";
        }

        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        //保存激活用户的昵称和用户名，下标0是用户名和手机号码
        WebElement user1Element = driver.findElement(AppiumBy.iOSClassChain(firstUser));
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //提取设置页面的手机号
        String user1PhoneAndUserName=nickNameAndUSerName.get(0).getAttribute("value");
        String user1Phone=user1PhoneAndUserName.substring(0,user1PhoneAndUserName.indexOf('@')-1).replace(" ", "");;
        System.out.println("设置页面显示的手机号是："+user1Phone);
        user1Element.click();

        //避免简介过长，上划一次
        Map<String, Object> argsUp = new HashMap<>();
        argsUp.put("direction", "up");
        driver.executeScript("mobile: swipe", argsUp);

        //点击账户设置
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("更换手机号码、更换邮箱"))).click();

        //设置页面的手机号元素
        WebElement phoneElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name CONTAINS '+'`]")));
        System.out.println("账户设置页面显示的手机号是："+phoneElement.getAttribute("value"));

        //和外面的手机号进行对比，如果和外面的手机号显示的不一致就报错
        if(!phoneElement.getAttribute("value").equals(user1Phone)){
            System.out.println("设置页面和账户设置页面的手机号不一致");
            driver.quit();
            return "设置页面和账户设置页面的手机号不一致";
        }

        //点击进入更换手机号提示页面
        phoneElement.click();

        //点击下一步
        System.out.println("开始点击下一步，并且输入的密码默认是123456");
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]"))).click();

        //输入两步验证,默认是123456
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeSecureTextField"))).sendKeys("123456");

        //点击下一步
        WebElement next = wait.until(ExpectedConditions.visibilityOfElementLocated
                (AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]")));
        wait.until(ExpectedConditions.attributeToBe(next, "enabled", "true"));//灰色的是false，变蓝了才能点击
        next.click();

        //点击旧手机号码输入框并输入
        WebElement oldPhoneElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeTextField[`value == \"旧手机号码\"`]")));
        oldPhoneElement.click();
        oldPhoneElement.sendKeys(user1Phone.substring(3));

        //点击新手机号码输入框并输入
        WebElement newPhoneElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain
                ("**/XCUIElementTypeTextField[`value == \"新手机号码\"`]")));
        newPhoneElement.click();
        newPhoneElement.sendKeys(newPhone);

        //点击右上角的下一步
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"下一步\"`]"))).click();

        //滑块验证需要自己手动验证
        System.out.println("10s内完成滑块验证");

        //输入默认的验证码  306908
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.className("XCUIElementTypeTextField"))).sendKeys("306908");

        //点击更换成功提示页右上角的完成按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("完成"))).click();

        //点击后返回到账户设置页面，查看手机号是否更换成功
        WebElement phoneElement1=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name CONTAINS '+'`]")));
        System.out.println("更换手机号后账户设置页面显示的手机号是："+phoneElement1.getAttribute("value"));

        //和更换的手机号进行对比，如果不一样就报错
        if(!phoneElement1.getAttribute("value").equals("+86"+newPhone)){
            System.out.println("更换手机号后设置页面显示的手机号和输入的不一致");
            driver.quit();
            return "更换手机号后设置页面显示的手机号和输入的不一致";
        }


        //点击返回
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("返回"))).click();

        WebElement firstUSerElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]")));


        //保存激活用户的昵称和用户名，下标0是用户名和手机号码
        List<WebElement> nickNameAndUSerNameElement=firstUSerElement.
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //提取设置页面的手机号
        String user1PhoneAndUserName1=nickNameAndUSerNameElement.get(0).getAttribute("value");
        String user1PhoneNew=user1PhoneAndUserName1.substring(0,user1PhoneAndUserName1.indexOf('@')-1).replace(" ", "");


        if(!user1PhoneNew.equals("+86"+newPhone)){
            System.out.println("最外层设置页面显示的账号和输入的号码不一致");
            return "最外层设置页面显示的账号和输入的号码不一致";
        }

        System.out.println("更换手机号码正常");
        driver.quit();
        return "更换手机号码正常";
    }


//feature10:个人资料-详情页面执行登录
    public String detailsPageLogin(String accountUser, String passwordUser) throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //先检查是不是登录上限了
        try{
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).size()==0){
                System.out.println("请前往主页面再开始程序");
                driver.quit();
                return "请前往主页面再开始程序";
            }

            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).click();
            //检查是否有第五个账号
            String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]";
            WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));
            if(user.getAttribute("name")==null){
                System.out.println("第5个账号已登录，请退出一个账号");
                driver.quit();
                return "第五个账号已登录，请退出一个账号";
            }
        }catch(TimeoutException | NoSuchElementException e){
            System.out.println("请在主页面重试");
            driver.quit();
            return "请在主页面重试";
        }

        String result=publicPartFunction2(driver,wait);
        if(!result.equals("正常")){
            return  result;
        }

        //避免简介过长，上划一次
        Map<String, Object> argsUp = new HashMap<>();
        argsUp.put("direction", "up");
        driver.executeScript("mobile: swipe", argsUp);

        //点击添加账号
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("添加账户"))).click();

        //执行登录
        String result1=Login.login(wait,driver,accountUser,passwordUser);
        if(!result1.equals("正常")){
            return  result1;
        }

        System.out.println("详情页面执行登录成功");
        driver.quit();
        return "详情页面执行登录成功";
    }


//feature11：个人资料-详情页面执行注册
    public String detailsPageRegistartion(String accountUser, String passwordUser) throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //先检查是不是登录上限了
        try{
            if(driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).size()==0){
                System.out.println("请前往主页面再开始程序");
                driver.quit();
                return "请前往主页面再开始程序";
            }

            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]")).click();
            //检查是否有第五个账号
            String userPath = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                    "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[5]";
            WebElement user=driver.findElement(AppiumBy.iOSClassChain(userPath));
            if(user.getAttribute("name")==null){
                System.out.println("第5个账号已登录，请退出一个账号");
                driver.quit();
                return "第五个账号已登录，请退出一个账号";
            }
        }catch(TimeoutException | NoSuchElementException e){
            System.out.println("请在主页面重试");
            driver.quit();
            return "请在主页面重试";
        }

        String result=publicPartFunction2(driver,wait);
        if(!result.equals("正常")){
            return  result;
        }

        //点击添加账号
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("添加账户"))).click();

        //执行注册
        String result1= Registartion.registartion(wait,driver,accountUser,passwordUser);
        if(!result1.equals("正常")){
            return  result1;
        }

        System.out.println("详情页面执行登录成功");
        driver.quit();
        return "详情页面执行登录成功";
}


//feature12:个人资料-详情页面退出账号
    public String detailsPageExitAccount() throws InterruptedException {
        Phione phione = new Phione();
        AppiumDriver driver = phione.driver;
        WebDriverWait wait = phione.wait;

        //检测页面数据的加载
        if(activateAccountDataLoadingInspect(wait,driver)){
            System.out.println("当前激活账户数据没有记载");
            driver.quit();
            return "当前激活账户数据没有记载";
        }

        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        //保存激活用户的昵称和用户名，下标0是用户名和手机号码
        WebElement firstUserElement = driver.findElement(AppiumBy.iOSClassChain(firstUser));
        List<WebElement> firstUserNickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

        //提取设置页面的手机号
        String firstUserPhoneAndUserName=firstUserNickNameAndUSerName.get(0).getAttribute("value");
        System.out.println("当前用户的手机号和用户名为："+firstUserPhoneAndUserName);
        //提取之后点击进入详情页面
        firstUserElement.click();
        Thread.sleep(1000);

        //避免简介过长，上划一次
        Map<String, Object> argsUp = new HashMap<>();
        argsUp.put("direction", "up");
        driver.executeScript("mobile: swipe", argsUp);

        //点击退出账号
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("退出账户"))).click();


        List<WebElement> deleteButton=driver.findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"删除\"`]"));
        if(deleteButton.size()==0){
            System.out.println("点击退出账户后没有出现弹窗");
            driver.quit();
            return "点击退出账户后没有出现弹窗";
        }

        //挨个点击删除按钮
        for (WebElement button : deleteButton) {
            button.click();
            Thread.sleep(500); // 可选：添加间隔防止操作过快
        }

        //点击确定
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"确定\"`]"))).click();
        Thread.sleep(1000);
        try{//如果没有历史页面则会进入到进入页面
            driver.findElement(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"进入\"`]"));
            System.out.println("详情页面退出账号成功");
            driver.quit();
            return "详情页面退出账号成功";
        }catch(NoSuchElementException e){
            //还有账号登录，进行对比
            //检测页面数据的加载
            if(activateAccountDataLoadingInspect(wait,driver)){
                System.out.println("当前激活账户数据没有记载");
                return "当前激活账户数据没有记载";
            }

            //查看切换账号后是不是之前的那个账号了
            List<WebElement> firstUserNickNameAndUSerNameNew=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                    findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));

            //提取设置页面的手机号
            String firstUserPhoneAndUserNameNew=firstUserNickNameAndUSerNameNew.get(0).getAttribute("value");
            System.out.println("退出账号后当前激活账户手机号和用户名为："+firstUserPhoneAndUserNameNew);

            //对比切换账号后保存的信息是否相同，不相同就是切换账号成功
            if(firstUserPhoneAndUserName.equals(firstUserPhoneAndUserNameNew)){
                System.out.println("切换账号后，账号信息没有变更，退出账号失败");
                return "切换账号后，账号信息没有变更，退出账号失败";
            }

            System.out.println("详情页面退出账号成功");
            driver.quit();
            return "详情页面退出账号成功";
        }
    }






//------------------下方的函数仅供上面的公有函数使用-------


//feature:这个是feature6、7、8、9、10、11的公共部分.     用于检测当前的账号，设置页面数据有没有加载
    private String publicPartFunction2(AppiumDriver driver, WebDriverWait wait) throws InterruptedException {
        //检测激活账号数据是否加载，数据没有加载就直接报错
        if (activateAccountDataLoadingInspect(wait, driver)) {
            System.out.println("当前激活账号数据没有加载");
            return "当前激活账号数据没有加载";
        }

        //用户列表中第一个用户的路径
        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";
        //检测当前账号是不是加载了
        WebElement user1 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUser)));
        user1.click();

        return "正常";
    }



//feature：使用预设头像进行设置，feature4的分支选项1
    private void usePresetAvatar(AppiumDriver driver,WebDriverWait wait){
        //预设头像所占的区域
        String avatarRegionPath="**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[2]/XCUIElementTypeOther[2]/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell" +
                "/XCUIElementTypeOther[2]";

        WebElement avatarRegionElement=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(avatarRegionPath)));

        // 创建 Actions 对象
        Actions actions = new Actions(driver);

        // 点击元素
        actions.click(avatarRegionElement).perform();

        // 向上滑动
        int startX = avatarRegionElement.getLocation().getX() + avatarRegionElement.getSize().getWidth() / 2;
        int startY = avatarRegionElement.getLocation().getY() + avatarRegionElement.getSize().getHeight() / 2;
        int endY = startY - 100; // 滑动的距离

        actions.moveToElement(avatarRegionElement, 0, 0)
                .clickAndHold()
                .moveByOffset(0, -100) // 向上滑动100像素
                .release()
                .perform();

        //选中那个蛇
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("/private/var/mobile/Containers/Shared/AppGroup" +
                "/C7ABBA13-B344-46E5-B53E-5D0FBE95B197/metadata/c642963a-59cd-4828-9ce6-c05451fe5a66/Library/Caches/AvatarBuilder" +
                "/75c4b81f5adfda8379c393b2d0b76070ec4f7b00.png"))).click();
    }


//feature：使用文本进行头像设置，feature4的分支选项2
    private void useTextAvatar(AppiumDriver driver,WebDriverWait wait){
        //点击文本按钮
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"文本\"`]"))).click();

        //照片中的输入框
        WebElement sendBox=wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]" +
                "/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther[1]/XCUIElementTypeImage/XCUIElementTypeTextField")));
        sendBox.sendKeys("t");//这个地方后面要写成随机的

        //点击完成
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("完成"))).click();
    }


//feature:使用照片进行头像设置，feature4的分支选项3
    private void usePhotoAlbum(AppiumDriver driver,WebDriverWait wait){
        //点击照片进入相册
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"图片\"`]"))).click();

        //默认选择第一张照片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeWindow[1]/XCUIElementTypeOther[3]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeCollectionView/XCUIElementTypeCell[1]" +
                "/XCUIElementTypeOther/XCUIElementTypeOther"))).click();

        //点击原图
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"原图\"`]"))).click();

        //点击确定
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"确定(1)\"`]"))).click();

        //点击完成
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"完成\"`]"))).click();
    }


//feature：使用相机拍照进行头像设置，feature4的分支选项4
    private void useCamera(AppiumDriver driver,WebDriverWait wait){
        //点击相机
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"相机\"`]"))).click();

        //点击翻转
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("FrontBackFacingCameraChooser"))).click();

        //点击拍照
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("PhotoCapture"))).click();

        //点击使用照片
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("使用照片"))).click();

        //点击完成
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText[`name == \"完成\"`]"))).click();
    }




//feature:从设置页面到点击更换照片，进入到更换照片页面停止   feature2、3、4的公共部分
    private String publicPartFunction1(AppiumDriver driver, WebDriverWait wait) throws InterruptedException {
        //检测激活账号数据是否加载，数据没有加载就直接报错
        if (activateAccountDataLoadingInspect(wait, driver)) {
            System.out.println("当前激活账号数据没有加载");
            return "当前激活账号数据没有加载";
        }

        //用户列表中第一个用户的路径
        String firstUser = "**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";

        WebElement user1 = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain(firstUser)));
        user1.click();


        //检测更换照片的按钮是不是存在并且能够点击
        try {
            WebElement changePhotoButton = wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.accessibilityId("更换照片")));
            changePhotoButton.click();
            Thread.sleep(1000);
            if (changePhotoButton.isDisplayed()) {
                System.out.println("更换照片按钮点击无反应");
                driver.quit();
                return "更换照片按钮点击无反应";
            }
        } catch (TimeoutException  | InterruptedException e) {
            System.out.println("更换照片按钮无法使用");
            driver.quit();
            return "更换照片按钮无法使用";
        }

        return "正常";
    }



//feature：检查当前激活账号数据是否加载的具体步骤，这个方法执行完就已经在设置页面了
    private boolean activateAccountDataLoadingInspect(WebDriverWait wait, AppiumDriver driver) throws InterruptedException {
        //首先点击设置页面
        wait.until(ExpectedConditions.visibilityOfElementLocated(AppiumBy.iOSClassChain("**/XCUIElementTypeButton[`name == \"设置\"`]"))).click();


        //用户列表中第一个用户的路径
        String firstUser="**/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeOther[2]/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther/XCUIElementTypeOther" +
                "/XCUIElementTypeOther/XCUIElementTypeTable/XCUIElementTypeCell[1]";


        //下滑三次第一个账户数据没有出现就是加载失败了
        int count=0;
        //如果第一个账号检测不出来就向下滑，直到出现为止
        do{
            Map<String, Object> argsDown = new HashMap<>();
            argsDown.put("direction", "down");
            driver.executeScript("mobile: swipe", argsDown);
            count++;
            if(count==3){
                break;
            }
        }while(driver.findElements(AppiumBy.iOSClassChain(firstUser)).size()==0);

        if(count>=3){
            return  true;
        }

        //保存下来当前激活账号的用户名和昵称元素
        List<WebElement> nickNameAndUSerName=driver.findElement(AppiumBy.iOSClassChain(firstUser)).
                findElements(AppiumBy.iOSClassChain("**/XCUIElementTypeStaticText"));
        if(nickNameAndUSerName.size()==0){
            return true;//数据真的不存在
        }
        else{
            for (WebElement element : nickNameAndUSerName) {
                System.out.println(element.getText());
            }
            return false;//数据已经加载了
        }
    }
}
